package com.rizzo.back.exception;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * ApplicationExceptions are used to provide feedback to the user interface.
 * <p/>
 * ApplicationExceptions contain a list of ApplicationExceptionItems that contain:
 * - a developer message
 * - a msgKey that refers to a key in a properties file with the visible error message
 * - the parameters used in that key
 *
 * @author Jan Lievens
 */
public class ApplicationException extends Exception {
    private static final long serialVersionUID = 5741863535200294413L;

    List<ApplicationExceptionItem> errors = new ArrayList<ApplicationExceptionItem>();

    /**
     * Constructs a new ApplicationException.
     */
    public ApplicationException() {
        super();
    }

    /**
     * Constructor ApplicationException creates a new ApplicationException instance.
     *
     * @param cause of type Throwable
     * @param errors of type ApplicationExceptionItem...
     */
    public ApplicationException(Throwable cause, ApplicationExceptionItem... errors) {
        super(cause);
        this.errors.addAll(Arrays.asList(errors));
    }

    /**
     * Constructs a new ApplicationException with multiple items.
     *
     * @param errors Multiple {@link ApplicationExceptionItem}.
     */
    public ApplicationException(ApplicationExceptionItem... errors) {
        super();
        this.errors.addAll(Arrays.asList(errors));
    }

    /**
     * @param developerMessage
     * @param msgKey
     * @param msgParams
     */
    public void addApplicationExceptionItem(String developerMessage, String msgKey, Object... msgParams) {
        errors.add(new ApplicationExceptionItem(developerMessage, msgKey, msgParams));
    }

    /**
     * @param applicationExceptionItem
     */
    public void addApplicationExceptionItem(ApplicationExceptionItem applicationExceptionItem) {
        errors.add(applicationExceptionItem);
    }

    /**
     * Getter for property 'applicationExceptionItems'.
     *
     * @return Value for property 'applicationExceptionItems'.
     */
    public List<ApplicationExceptionItem> getApplicationExceptionItems() {
        return errors;
    }

    /**
     * Checks if there are any {@link com.rizzo.back.exception.ApplicationExceptionItem}s attached to
     * this {@link com.rizzo.back.exception.ApplicationException}.
     *
     * @return A boolean stating if this {@link com.rizzo.back.exception.ApplicationException} has
     *         {@link com.rizzo.back.exception.ApplicationExceptionItem}s.
     */
    public boolean hasItems() {
        return errors.size() > 0;
    }
}